package com.eqdd.yiqidian.rongyun.message.link;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.rongyun.message.mingpian.MingpianMessage;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.web.WebActivity;

import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * Created by lvzhihao on 17-4-30.
 */
@ProviderTag(
        messageContent = LinkMessage.class,
        showReadState = true
)
public class LinkMessageItemProvider extends IContainerItemProvider.MessageProvider<LinkMessage> {

    class ViewHolder {
        LinearLayout linearLayout;
        TextView textViewTitle;
        TextView textViewContent;
        TextView textViewSource;
        ImageView imageView;
    }

    private Context context;

    @Override
    public View newView(Context context, ViewGroup group) {
        this.context = context;
        ViewDataBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.item_link_message, null, false);
        View view = inflate.getRoot();
        ViewHolder holder = new ViewHolder();
        holder.textViewTitle = view.findViewById(R.id.tv_title);
        holder.textViewContent = view.findViewById(R.id.tv_content);
        holder.textViewSource = view.findViewById(R.id.tv_source);
        holder.imageView = view.findViewById(R.id.image);
        holder.linearLayout = view.findViewById(R.id.ll_root);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View v, int position, LinkMessage content, UIMessage message) {
        ViewHolder holder = (ViewHolder) v.getTag();

        if (message.getMessageDirection() == Message.MessageDirection.SEND) {//消息方向，自己发送的
            holder.linearLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
        } else {
            holder.linearLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);
        }
        holder.textViewTitle.setText(content.getContent().getTitle());
        holder.textViewContent.setText(content.getContent().getContent());
        holder.textViewSource.setText(content.getContent().getSource());
        if (TextUtils.isEmpty(content.getContent().getTitle())) {
            holder.textViewTitle.setVisibility(View.GONE);
        }
        ImageUtil.setImage(content.getContent().getImgUrl(), holder.imageView);
    }


    @Override
    public Spannable getContentSummary(LinkMessage data) {
        return new SpannableString("[分享]" + data.getContent().getTitle());
    }


    @Override
    public void onItemClick(View view, int position, LinkMessage content, UIMessage message) {

        Intent intent = new Intent(this.context, WebActivity.class);
        intent.putExtra(Config.TITLE, content.getContent().getTitle());
        intent.putExtra(Config.URL, content.getContent().getUrl());
        intent.putExtra(Config.IS_LOGIN, true);
        this.context.startActivity(intent);

    }

    @Override
    public void onItemLongClick(View view, int position, LinkMessage content, UIMessage message) {

    }

}